/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * cambiarEstado.java
 *
 * Created on 26-abr-2011, 2:50:49
 */

package interfaces;

import GestorUsuarios.EstadoUsuario;
import GestorUsuarios.Usuario;
import clinica.ClinicaView;
import clinica.Interfaces;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import org.jdesktop.application.Action;
import Controlador.*;
import GestorUsuarios.TipoUsuario;
import clinica.Interfaz;

/**
 *
 * @author Victor
 */
public class cambiarEstado extends javax.swing.JPanel {

    private ClinicaView app;
    private Usuario usuario;

    /** Creates new form cambiarEstado */
    public cambiarEstado(ClinicaView a) {
        initComponents();
        app = a;
    }

    public void rellenaDatos(Usuario u) {
        usuario = u;
        activo.setEnabled(true);
        definitiva.setEnabled(true);
        if(usuario.getTipo() == TipoUsuario.PACIENTE)
            temporal.setEnabled(false);
        else temporal.setEnabled(true);
        nomusu.setText(u.getNombre()+" "+u.getApellidos());
        if(usuario.getEstado()==EstadoUsuario.ACTIVO)
            activo.setSelected(true);
        else if(usuario.getEstado()==EstadoUsuario.BAJA_TEMPORAL)
            temporal.setSelected(true);
        else definitiva.setSelected(true);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        estados = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        nomusu = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        temporal = new javax.swing.JRadioButton();
        activo = new javax.swing.JRadioButton();
        definitiva = new javax.swing.JRadioButton();
        aceptar = new javax.swing.JButton();
        cancelar = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(cambiarEstado.class);
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel2.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize())); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N

        jLabel1.setName("jLabel1"); // NOI18N

        nomusu.setFont(Interfaz.setFontSize());
        nomusu.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        nomusu.setIcon(resourceMap.getIcon("nomusu.icon")); // NOI18N
        nomusu.setText(resourceMap.getString("nomusu.text")); // NOI18N
        nomusu.setName("nomusu"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jLabel1))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(nomusu, javax.swing.GroupLayout.DEFAULT_SIZE, 720, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(nomusu)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel5.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize())); // NOI18N
        jPanel5.setName("jPanel5"); // NOI18N
        jPanel5.setLayout(new java.awt.GridLayout(1, 3));

        estados.add(temporal);
        temporal.setFont(Interfaz.setFontSize());
        temporal.setText(resourceMap.getString("temporal.text")); // NOI18N
        temporal.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        temporal.setName("temporal"); // NOI18N
        jPanel5.add(temporal);

        estados.add(activo);
        activo.setFont(Interfaz.setFontSize());
        activo.setText(resourceMap.getString("activo.text")); // NOI18N
        activo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        activo.setName("activo"); // NOI18N
        jPanel5.add(activo);

        estados.add(definitiva);
        definitiva.setFont(Interfaz.setFontSize());
        definitiva.setText(resourceMap.getString("definitiva.text")); // NOI18N
        definitiva.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        definitiva.setName("definitiva"); // NOI18N
        jPanel5.add(definitiva);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(cambiarEstado.class, this);
        aceptar.setAction(actionMap.get("aceptar")); // NOI18N
        aceptar.setIcon(resourceMap.getIcon("aceptar.icon")); // NOI18N
        aceptar.setName("aceptar"); // NOI18N

        cancelar.setAction(actionMap.get("cancelar")); // NOI18N
        cancelar.setIcon(resourceMap.getIcon("cancelar.icon")); // NOI18N
        cancelar.setName("cancelar"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 756, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(aceptar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(aceptar)
                    .addComponent(cancelar))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void cancelar() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException {
        app.volver(Interfaces.BUSCAR_USUARIOS2);
    }

    @Action
    public void aceptar() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException {
        EstadoUsuario estado = null;
        if(activo.isSelected()) estado = EstadoUsuario.ACTIVO;
        else if(temporal.isSelected()) estado = EstadoUsuario.BAJA_TEMPORAL;
        else if(definitiva.isSelected()) estado = EstadoUsuario.BAJA_DEFINITIVA;

        usuario.setEstado(estado);
        Usuario.update(usuario);
        
         app.volver(Interfaces.BUSCAR_USUARIOS);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptar;
    private javax.swing.JRadioButton activo;
    private javax.swing.JButton cancelar;
    private javax.swing.JRadioButton definitiva;
    private javax.swing.ButtonGroup estados;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JLabel nomusu;
    private javax.swing.JRadioButton temporal;
    // End of variables declaration//GEN-END:variables

}
